package com.itheima.tlias.controller;

import com.itheima.tlias.common.PageBean;
import com.itheima.tlias.common.Result;
import com.itheima.tlias.pojo.Emp;
import com.itheima.tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工功能
 * @author
 * @version 1.0.1
 * @date 2023-04-08 10:01:38
 */
@RestController
@RequestMapping("/emps")
@Slf4j
public class  EmpController {

    @Autowired
    private EmpService empService;

    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.warn("emp:{}", emp);
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(name = "id") Integer id){
        log.warn("id:{}", id);
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }


    /**
     * 新增员工
     * @param emp
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.warn("emp:{}", emp);
        empService.add(emp);
        return Result.success();
    }

    /**
     * 根据ID批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable(name = "ids") List<Integer> ids){
        log.warn("ids:{}", ids);
        //TODO 调用业务
        empService.delete(ids);
        return Result.success();
    }


    @GetMapping
    public Result page(
            String name, Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,  //yyyy-MM-dd
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        //int i = 1/0;
        log.warn("name:{}, gender:{}, begin:{}, end:{}, page:{}, pageSize:{},", name, gender, begin, end, page, pageSize);
        //调用业务接口
        PageBean<Emp> pageBean = empService.page(name, gender, begin, end, page, pageSize);
        return Result.success(pageBean);
    }

}
